<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:ui="http://java.sun.com/jsf/facelets">

<ui:composition template="template.xhtml">
	<ui:define name="corpo">

		<h2>Questaos</h2>
		<h:panelGrid>

			<p:commandButton value="Adicionar Questao"
				oncomplete="dialogCadastroQuestao.show()" />
		</h:panelGrid>

		<h:form id="cadQuestao">

			<p:dialog widgetVar="dialogCadastroQuestao" header="Dados da Questão"
				closable="false" resizable="false" modal="true" hideEffect="slide"
				showEffect="clip" draggable="false">

				<h:panelGrid id="displayQuestao" columns="3" cellpadding="4"
					style="margin:0 auto;">

					<h:outputLabel value="Enunciado: " />
					<p:inputTextarea id="nome"
						value="#{cadastroBean.questao.enunciado}"
						title="Insira aqui o enunciado" />
					<p:message for="nome"></p:message>

					<h:outputText></h:outputText>
					<p:commandButton id="btsalvar" value="Salvar"
						rendered="#{cadastroBean.questao.id == null}"
						actionListener="#{cadastroBean.gravar}"
						update=":formulario:tabelaQuestao displayQuestao"
						oncomplete="handleCadastroQuestaoRequest(xhr, status, args)">
					</p:commandButton>

					<p:commandButton id="btAlterar" value="Alterar"
						rendered="#{cadastroBean.questao.id != null}"
						actionListener="#{cadastroBean.alterar}"
						update=":formulario:tabelaQuestao displayQuestao"
						oncomplete="handleCadastroQuestaoRequest(xhr, status, args)">
					</p:commandButton>

					<p:commandButton value="Voltar" update="displayQuestao"
						process="@this" oncomplete="dialogCadastroQuestao.hide()"
						action="#{cadastroBean.limpar}">
						<p:resetInput target="displayQuestao" />
					</p:commandButton>
				</h:panelGrid>

			</p:dialog>

			<script type="text/javascript">
				function handleCadastroQuestaoRequest(xhr, status, args) {
					if (args.validationFailed || !args.gravado) {
						jQuery('#dialogCadastroQuestao').effect("shake", {
							times : 3
						}, 100);
					} else {
						dialogCadastroQuestao.hide();
					}
				}
			</script>

		</h:form>



		<h:form id="formulario">

			<p:dataTable id="tabelaQuestao" value="#{cadastroBean.questoes}"
				var="questao"
				rowStyleClass="#{empty rowIx or rowIx mod 2 ne 0 ? 'par' : 'impar'}"
				rowIndexVar="rowIx" emptyMessage="nenhum registro encontrado">

				<p:column
					rendered="#{questao.alternativas.size() != 0 and questao.alternativas.size() != 1 and questao.alternativas.size() != null}">
					<div style="color: blue">
						<h:outputText
							value="#{questao.enunciado} #{questao.alternativas.size()}" />
					</div>
				</p:column>

				<p:column
					rendered="#{questao.alternativas.size() == 0 or questao.alternativas.size() == 1 or questao.alternativas.size() == null}">
					<div style="color: blue">
						<h:outputText value="#{questao.enunciado}" />
					</div>
					<div style="color: red">
						<h:outputText value="Favor adicionar pelo menos duas questões do contrário ela não aparecerá no questionário" />
					</div>
				</p:column>



				<p:column headerText="Ações">
					<div align="center">
						<p:commandLink update=":cadQuestao:displayQuestao" title="Editar"
							oncomplete="dialogCadastroQuestao.show()">
							<f:setPropertyActionListener value="#{questao}"
								target="#{cadastroBean.questao}" />
							<p:graphicImage value="resources/imagens/editar.png" width="24px"
								height="24px" />
						</p:commandLink>

						<p:commandLink update=":formAdicionaALternativa"
							title="Adicionar alternativa" immediate="true"
							oncomplete="dialogCadastroAlternativa.show()"
							action="#{cadastroBean.preencheAlternativas}">
							<f:setPropertyActionListener value="#{questao}"
								target="#{cadastroBean.questao}" />
							<p:graphicImage value="resources/imagens/editar.png" width="24px"
								height="24px" />
						</p:commandLink>


						<p:commandLink update=":formRemoveQuestao:panelRemoveQuestao"
							title="Remover" oncomplete="popupRemoveQuestao.show()">
							<f:setPropertyActionListener value="#{questao}"
								target="#{cadastroBean.questao}" />
							<p:graphicImage value="resources/imagens/delete.png" width="24px"
								height="24px" />
						</p:commandLink>
					</div>
				</p:column>
			</p:dataTable>
		</h:form>

		<h:form id="formRemoveQuestao">
			<p:dialog header="Têm certeza que deseja apagar?"
				widgetVar="popupRemoveQuestao" resizable="false"
				id="popupRemoveQuestao" showEffect="fade" hideEffect="explode"
				closable="false" modal="true">

				<p:panel id="panelRemoveQuestao" columns="2">

					<p:commandButton value="Sim, remover"
						action="#{cadastroBean.remover}"
						update=":formulario:tabelaQuestao"
						oncomplete="popupRemoveQuestao.hide()" />

					<p:commandButton value="Não remover"
						action="#{cadastroBean.limpar}"
						onclick="popupRemoveQuestao.hide()" />
				</p:panel>
			</p:dialog>
		</h:form>

		<h:form id="formAdicionaALternativa">

			<p:dialog widgetVar="dialogCadastroAlternativa"
				header="Adicionar alternativa" closable="false" resizable="false"
				modal="true" hideEffect="slide" showEffect="clip" draggable="true">

				<p:panelGrid id="displayAlternativa" columns="3" cellpadding="4"
					style="margin:0 auto;" title="#{cadastroBean.questao.enunciado}">

					<f:facet name="header">#{cadastroBean.questao.enunciado}</f:facet>

					<h:outputLabel value="Alternativa: " />
					<p:inputText id="descAlt"
						value="#{cadastroBean.alternativa.descricao}"
						title="Insira aqui a descrição da alternativa" />
					<p:commandButton id="btsalvar" value="Adicionar"
						action="#{cadastroBean.addAlternativa}"
						update=":formAdicionaALternativa:tableAlternativas :formAdicionaALternativa:displayAlternativa"></p:commandButton>
					<h:outputText></h:outputText>
					<p:message for="descAlt" />
					<h:outputText></h:outputText>



				</p:panelGrid>
				<p:dataTable id="tableAlternativas"
					value="#{cadastroBean.alternativas}" var="alt"
					rowStyleClass="#{empty rowIx or rowIx mod 2 ne 0 ? 'par' : 'impar'}"
					rowIndexVar="rowIx" emptyMessage="nenhuma alternativa cadastrada">

					<p:column>
						<h:outputText value="#{alt.descricao}" />
					</p:column>

					<p:column>
						<p:commandButton value="remover" immediate="true"
							oncomplete="removeAlternativa.show()"
							update=":formApaga:apagaAlt">
							<f:setPropertyActionListener target="#{cadastroBean.alternativa}"
								value="#{alt}" />
						</p:commandButton>
					</p:column>
				</p:dataTable>
				<h:outputText></h:outputText>
				<p:commandButton value="fechar" immediate="true"
					onclick="dialogCadastroAlternativa.hide()"
					actionListener="#{cadastroBean.limparAlternativa}"
					action="#{cadastroBean.limpar}" update=":formulario:tabelaQuestao" />


			</p:dialog>

		</h:form>

		<h:form id="formApaga">
			<p:dialog widgetVar="removeAlternativa" resizable="false"
				id="apagCompDialog" showEffect="fade" hideEffect="explode"
				closable="false" modal="true">

				<h:panelGrid id="apagaAlt" columns="2" cellpadding="4"
					style="margin:0 auto;">

					<p:commandButton value="Sim, remover"
						actionListener="#{cadastroBean.removeAlternativa}"
						update=":formAdicionaALternativa:tableAlternativas"
						oncomplete="removeAlternativa.hide()"
						action="#{cadastroBean.preencheAlternativas}" />

					<p:commandButton value="Não remover"
						onclick="removeAlternativa.hide()" />
				</h:panelGrid>
			</p:dialog>
		</h:form>

	</ui:define>
</ui:composition>
</html>